package com.example.album;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpSession;

@Controller
public class AlbumManagementHandler {
    @Autowired
    private AlbumManagementService ams;
    @Autowired
    private ImgManagementService ims;
    @Autowired
    private CategoryRepository cr;
    @Autowired
    private HttpSession session;

    public String createAlbum(String albumName, String categoryid) {
        String userid = (String) session.getAttribute("userid");
        Album album = ams.createAlbum(userid, albumName, categoryid);
        return album.getAlbumId();
    }

    @GetMapping("getcategorylist")
    @ResponseBody
    public List<String> getCategoryList() {
        System.out.println("getCategoryList called");
        List<String> categories = ams.getAllCategories();
        for (String category : categories) {
            System.out.println("Category: " + category);
        }
        return ams.getAllCategories();
    }

    @PostMapping("Createalbum")
    @ResponseBody
    public Map<String, Object> handleCreateAlbum(@RequestBody Map<String, String> req) {
        System.out.println("handleCreateAlbum called with title: " + req.get("title") + " and categoryName: " + req.get("categoryname"));
        createAlbum(req.get("title"), req.get("categoryname"));
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }
    @GetMapping("getalbuminfo/{albumId}")
    @ResponseBody
    public Map<String, Object> getAlbumInfo(@PathVariable String albumId) {
        
        return ams.getAlbumInfo(albumId);
    }

    @PostMapping("renamealbum")
    @ResponseBody
    public Map<String, Object> renameAlbum(@RequestBody Map<String, String> req) {
        String albumId = req.get("albumId");
        String newTitle = req.get("newTitle");
        ams.renameAlbum(albumId, newTitle);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("changealbumcategory")
    @ResponseBody
    public Map<String, Object> changeAlbumCategory(@RequestBody Map<String, String> req) {
        String albumId = req.get("albumId");
        String newCategory = req.get("newCategory");
        ams.changeAlbumCategory(albumId, newCategory);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("deletealbum")
    @ResponseBody
    public Map<String, Object> deleteAlbum(@RequestBody Map<String, String> req) {
        String albumId = req.get("albumId");
        ams.deleteAlbum(albumId);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("uploadphotos")
    @ResponseBody
    public Map<String, Object> uploadPhotos(
        @RequestParam("albumId") String albumId,
        @RequestParam("photos") List<MultipartFile> photos,
        HttpSession session
    ) {
        ims.uploadPhotos(albumId, photos, session);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("deletephoto")
    @ResponseBody
    public Map<String, Object> deletePhoto(@RequestBody Map<String, String> req) {
        String albumId = req.get("albumId");
        String photoId = req.get("photoId");
        ims.deletePhoto(albumId, photoId);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("setcover")
    @ResponseBody
    public Map<String, Object> setCover(@RequestBody Map<String, String> req) {
        String albumId = req.get("albumId");
        String photoId = req.get("photoId");
        ams.setAlbumCover(albumId, photoId);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @GetMapping("getallalbumsbrief")
    @ResponseBody
    public List<Map<String, String>> getAllAlbumsBrief() {
        return ams.getAllAlbumsBrief();
    }

    @GetMapping("getmyalbumsbrief")
    @ResponseBody
    public List<Map<String, String>> getMyAlbumsBrief() {
        return ams.getMyAlbumsBrief();
    }

    

    @GetMapping("getcollectedalbums")
    @ResponseBody
    public List<Map<String, String>> getCollectedAlbums() {
        String userId = (String) session.getAttribute("userid");
        return ams.getCollectedAlbumsBrief(userId);
    }
}